// interface of module 'msg_mark'.
#include "msg_mark.h"

// import interface.
#include <windows.h>
#include <assert.h>
#include <stdio.h>

struct msg_markTag {
	FILE* pf;
};


// return NULL if failure.
struct msg_markTag* init_msg_mark(const char* filename) {
	static char* fmt = ".\\%s";
	FILE* pf;
	char buf[MAX_PATH];
	struct msg_markTag* obj;

	assert(filename);

	sprintf(buf, fmt, filename);
	pf = fopen(buf, "wb");
	if (pf == NULL) {
		return NULL;
	}
	
	obj = calloc(1, sizeof(struct msg_markTag));
	if (obj == NULL) {
		fclose(pf);
		return NULL;
	}

	obj->pf = pf;
	return obj;
}

void release_msg_mark(struct msg_markTag* obj) {
	assert(obj);
	fclose(obj->pf);
	free(obj);
}

void _mark(struct msg_markTag* obj , const unsigned int code, const char* msg, const char* szFile, unsigned int uLine) {
	char buf[1024];
	assert(obj);
	sprintf(buf, "File: %s, Line: %u, Msg: \'%s\', Code: %u.\r\n", szFile, uLine, msg, code);
	printf(buf);
	fprintf(obj->pf, buf);
	fflush(obj->pf);
}
